package entities.userstate;

import entities.User;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@Entity
@Table(name = "cancelled_user")
@NamedQueries({
    @NamedQuery(name = "CancelledUser.findAll", query = "SELECT c FROM CancelledUser c"),
    @NamedQuery(name = "CancelledUser.findByIdUser", query = "SELECT c FROM CancelledUser c WHERE c.idUser = :idUser"),
    @NamedQuery(name = "CancelledUser.findByCancelingReason", query = "SELECT c FROM CancelledUser c WHERE c.cancelingReason = :cancelingReason"),
    @NamedQuery(name = "CancelledUser.findByCancelingDate", query = "SELECT c FROM CancelledUser c WHERE c.cancelingDate = :cancelingDate")})
public class CancelledUser implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "idUser")
    private Integer idUser;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "unlockAccount")
    private boolean unlockAccount;
    
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 300)
    @Column(name = "cancelingReason")
    private String cancelingReason;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "cancelingDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date cancelingDate;
    
    @JoinColumn(name = "idUser", referencedColumnName = "idUser", insertable = false, updatable = false)
    @OneToOne(optional = false)
    private User user;

    public CancelledUser() {
    }
    
    public CancelledUser(User user, String cancelingReason, Date cancelingDate) {
        this.user = user;
        if(cancelingReason == null || cancelingReason.isEmpty()) {
            cancelingReason = " ";
        }
        this.cancelingReason = cancelingReason;
        this.cancelingDate = cancelingDate;
        this.idUser = user.getId();
        this.unlockAccount = false;
    }

    public Integer getIdUser() {
        return idUser;
    }

    public String getCancelingReason() {
        return cancelingReason;
    }

    public Date getCancelingDate() {
        return cancelingDate;
    }

    public User getUser() {
        return user;
    }
    
    public void unlockAccount() {
        this.unlockAccount = true;
    }
    
    public boolean isUnlocked() {
        return unlockAccount;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idUser != null ? idUser.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof CancelledUser)) {
            return false;
        }
        CancelledUser other = (CancelledUser) object;
        return !((this.idUser == null && other.idUser != null) || (this.idUser != null && !this.idUser.equals(other.idUser)));
    }

    @Override
    public String toString() {
        return "entities.extra.CancelledUser[ idUser=" + idUser + " ]";
    }

}
